<template>
  <div>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['client:corp-ep-equip-info:add']"
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['client:corp-ep-equip-info:delete']"
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="selected.length === 0"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['client:corp-ep-equip-info:import']"
          type="success"
          plain
          icon="el-icon-upload"
          size="mini"
          @click="$refs.file.click()"
        >导入</el-button>
        <input ref="file" type="file" style="display: none" @change="handleUpload">
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['client:corp-ep-equip-info:export']"
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleDownload"
        >导入模板下载</el-button>
      </el-col>
    </el-row>

    <el-table
      ref="table"
      v-loading="loading"
      :data="list"
      @selection-change="(selection) => selected = selection"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="设备名称" prop="equipName" />
      <el-table-column label="设备数量" prop="equipNum" />
      <el-table-column label="设备数据来源" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.ep_baisc_equip_source" :value="scope.row.equipSource" />
        </template>
      </el-table-column>
      <el-table-column label="状态" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_data_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="修改人">
        <template slot-scope="scope">
          <span>{{ scope.row.modifyBy || '-' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改时间">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime) || '-' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['client:corp-ep-equip-info:update']"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            v-hasPermi="['client:corp-ep-equip-info:delete']"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-form-item label="设备名称" prop="equipName">
          <el-input v-model="form.equipName" placeholder="请输入设备名称" />
        </el-form-item>
        <el-form-item label="设备数量" prop="equipNum">
          <el-input-number v-model="form.equipNum" :min="1" placeholder="请输入设备数量" />
        </el-form-item>
        <el-form-item label="设备数据来源" prop="equipSource">
          <el-select
            v-model="form.equipSource"
            style="width: 240px"
          >
            <el-option
              v-for="dict in dict.type.ep_baisc_equip_source"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="status" required>
          <el-select
            v-model="form.status"
            style="width: 240px"
          >
            <el-option
              v-for="dict in dict.type.sys_data_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { statusValidator } from '@/utils/validate'
import { listEquipment, getEquipment, delEquipment, addEquipment, updateEquipment, importEquipment } from '@/api/client/equipment'

export default {
  name: 'Equipment',
  dicts: ['sys_data_status','ep_baisc_equip_source'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      selected: [],
      // 总条数
      total: 0,
      // 角色表格数据
      list: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10
      },
      // 表单参数
      form: {
        status: 0,
        equipSource: '环评'
      },
      // 表单校验
      rules: {
        equipName: [
          { required: true, message: '设备名称不能为空', trigger: 'blur' }
        ],
        equipNum: [
          { type: 'number', required: true, message: '设备数量不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询角色列表 */
    getList() {
      this.loading = true
      listEquipment({
        ...this.queryParams,
        corpId: this.$route.params.id
      }).then(response => {
        this.list = response.data.records
        this.total = response.data.total
        this.loading = false
      }
      )
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        status: 0,
        equipSource: '环评'
      }
      this.resetForm('form')
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '新增设备'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      getEquipment(row.id).then(response => {
        this.form = {
          ...response.data,
          status: `${response.data.status}`
        }
        this.open = true
        this.title = '修改设备信息'
      })
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          const formData = {
            id: '',
            ...this.form,
            status: parseInt(this.form.status),
            corpId: this.$route.params.id
          }
          if (this.form.id) {
            updateEquipment(formData).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addEquipment(formData).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.selected.map(v => v.id)
      this.$modal.confirm(`确认删除 ${row.id ? row.equipName : '所选'} 数据？`).then(function() {
        return delEquipment(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
        this.$refs.table.clearSelection()
        this.selected = []
      }).catch(() => {})
    },
    async handleUpload(event) {
      const data = new FormData()
      data.append('file', event.target.files[0])
      await importEquipment(data)
      this.getList()
    },
    handleExport() {
      this.download('/security/corpEpEquipInfo/export', {}, `企业主要生产设备_${new Date().getTime()}.xlsx`)
    },
    handleDownload() {
      this.download('/security/corpEpEquipInfo/importTemplate', {}, `企业主要生产设备模板.xlsx`)
    }
  }
}
</script>
